/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package replica_manager_client;

import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.*;
import javax.xml.rpc.encoding.XMLType;
import ws_client.ParameterSpecification;
import ws_client.WSAccessor;


/**
 *
 * @author iMarshall
 */
public class ReplicaManagerAccessor extends WSAccessor
{
    public ReplicaManagerAccessor()
    {
        super();
    }

    // NOTE: provided endpoint is without ?wsdl appended
    // eg: http://localhost:8080/ReplicaManager-war/ReplicaManagerWSService
    public ReplicaManagerAccessor(String endpoint)
    {
        this();
        this.endpoint = endpoint;
        serviceName = new QName(endpoint + "?wsdl", "ReplicaManagerWSService");
        namespace = "http://replica_manager_ws/";
    }

    public boolean retrieveReplica()
        throws ServiceException, RemoteException
    {
        ParameterSpecification[] ps = { };
        Object[] args = { };

        Object returnValue =
            callOperation("retrieveReplica", ps, args, XMLType.XSD_BOOLEAN);
        Boolean ret = Boolean.parseBoolean(returnValue.toString());

        return ret.booleanValue();
    }

    public boolean submitReplica(String resource)
        throws ServiceException, RemoteException
    {
        ParameterSpecification[] ps =
        {
            new ParameterSpecification(
                "resource",
                XMLType.XSD_STRING,
                String.class,
                ParameterMode.IN)
        };

        Object[] args = { resource };
        
        Object returnValue =
            callOperation("submitReplica", ps, args, XMLType.XSD_BOOLEAN);
        Boolean ret = Boolean.parseBoolean(returnValue.toString());

        return ret.booleanValue();
    }

    public boolean getReplica(String tID)
        throws ServiceException, RemoteException
    {
        ParameterSpecification[] ps =
        {
            new ParameterSpecification(
                "tID",
                XMLType.XSD_STRING,
                String.class,
                ParameterMode.IN)
        };

        Object[] args = { tID };

        Object returnValue =
            callOperation("getReplica", ps, args, XMLType.XSD_BOOLEAN);
        Boolean ret = Boolean.parseBoolean(returnValue.toString());

        return ret.booleanValue();
    }


    public String transmitReplica(String tID)
        throws ServiceException, RemoteException
    {
        ParameterSpecification[] ps =
        {
            new ParameterSpecification(
                "tID",
                XMLType.XSD_STRING,
                String.class,
                ParameterMode.IN)
        };

        Object[] args = { tID };

        Object returnValue =
            callOperation("transmitReplica", ps, args, XMLType.XSD_STRING);

        return returnValue.toString();
    }

    public boolean pushReplica(String resource, String tID)
        throws ServiceException, RemoteException
    {
        ParameterSpecification[] ps =
        {
            new ParameterSpecification(
                "resource",
                XMLType.XSD_STRING,
                String.class,
                ParameterMode.IN),
                
            new ParameterSpecification(
                "tID",
                XMLType.XSD_STRING,
                String.class,
                ParameterMode.IN)
        };

        Object[] args = { resource, tID };

        Object returnValue =
            callOperation("pushReplica", ps, args, XMLType.XSD_BOOLEAN);
        Boolean ret = Boolean.parseBoolean(returnValue.toString());

        return ret.booleanValue();
    }

    public boolean rollBack(String tID)
        throws ServiceException, RemoteException
    {
        ParameterSpecification[] ps =
        {
            new ParameterSpecification(
                "tID",
                XMLType.XSD_STRING,
                String.class,
                ParameterMode.IN)
        };

        Object[] args = { tID };

        Object returnValue =
            callOperation("rollBack", ps, args, XMLType.XSD_BOOLEAN);
        Boolean ret = Boolean.parseBoolean(returnValue.toString());

        return ret.booleanValue();
    }

    public boolean canCommit(String tID)
        throws ServiceException, RemoteException
    {
        ParameterSpecification[] ps =
        {
            new ParameterSpecification(
                "tID",
                XMLType.XSD_STRING,
                String.class,
                ParameterMode.IN)
        };

        Object[] args = { tID };

        Object returnValue =
            callOperation("canCommit", ps, args, XMLType.XSD_BOOLEAN);
        Boolean ret = Boolean.parseBoolean(returnValue.toString());

        return ret.booleanValue();
    }

    public boolean doCommit(String tID)
        throws ServiceException, RemoteException
    {
        ParameterSpecification[] ps =
        {
            new ParameterSpecification(
                "tID",
                XMLType.XSD_STRING,
                String.class,
                ParameterMode.IN)
        };

        Object[] args = { tID };

        Object returnValue =
            callOperation("doCommit", ps, args, XMLType.XSD_BOOLEAN);
        Boolean ret = Boolean.parseBoolean(returnValue.toString());

        return ret.booleanValue();
    }
}
